import {
  type FilterItem,
  formatValue,
  type SearchValue
} from '@/components/business/src/StrongSearch'

export default function useStrongSearch(
  filterList: FilterItem[],
  queryParams: object
) {
  function handleStrongSearch(arr: SearchValue[]) {
    const params = formatValue(arr)
    filterList.forEach(item => {
      if (item.popover === 'date') {
        item.dateFields!.forEach(v => {
          Reflect.set(queryParams, v, params[v])
        })
      } else {
        let value
        if (item.fieldType === 'number') {
          value = Number(params[item.type]) || undefined
        } else {
          value = params[item.type]
        }
        Reflect.set(queryParams, item.type, value)
      }
    })
  }

  return { handleStrongSearch }
}
